﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using System.Text.RegularExpressions;

namespace FoundationLib.Web.Mvc
{
	/// <summary>
	/// Provides helper methods for expressions.
	/// </summary>
	public static class ExpressionHelper
	{
		/// <summary>
		/// Resolves the property path of the given property expression.
		/// </summary>
		/// <typeparam name="TEntity">The type containing the property</typeparam>
		/// <typeparam name="TProperty">The returning type of the property</typeparam>
		/// <param name="expression">The property expression</param>
		/// <returns>A string representing the property paty</returns>
		public static string PropertyPathFor<TEntity, TProperty>(Expression<Func<TEntity, TProperty>> expression)
		{
			Match match = Regex.Match(expression.ToString(), @"^[^\.]+\.([^\(\)]+)$");
			return match.Groups[1].Value;
		}
	}
}
